using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace stsdev {

  public partial class SelectSigningKey : Form {
  
    public SelectSigningKey() {
      InitializeComponent();
    }
   
    public string  SigningKeyFile {
      get { return txtSigningKey.Text; }
    }

    private void SelectSigningKey_Load(object sender, EventArgs e) {
      // load default signing key
      RegistryKey defaultSigningKeyFileKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
      object solutionSigningKeyFile = defaultSigningKeyFileKey.GetValue("");
      if (solutionSigningKeyFile != null && File.Exists(solutionSigningKeyFile.ToString())) {
        txtSigningKey.Text = solutionSigningKeyFile.ToString();
      }
      else {
        cmdOK.Enabled = false;
      }
    }

    private void cmdCreateNewKey_Click(object sender, EventArgs e) {
      dlgSaveFileDialog.DefaultExt = "snk";
      dlgSaveFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";  
      DialogResult result = dlgSaveFileDialog.ShowDialog();
      if (result == DialogResult.OK) {
        string KeyFileName = dlgSaveFileDialog.FileName;
        StrongNameKeyBuilder.CreateNew(KeyFileName);
        txtSigningKey.Text = KeyFileName;
        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
        defaultFolderKey.SetValue("", KeyFileName);
      }
    }

    private void cmdChangeSigningKey_Click(object sender, EventArgs e) {
      dlgOpenFileDialog.CheckFileExists = true;
      dlgOpenFileDialog.DefaultExt = "snk";
      dlgOpenFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";
      dlgOpenFileDialog.InitialDirectory = @"c:\";
      dlgOpenFileDialog.Title = "Select a signing key file";
      DialogResult result = dlgOpenFileDialog.ShowDialog();
      if (result == DialogResult.OK) {
        string KeyFileName = dlgOpenFileDialog.FileName;
        txtSigningKey.Text = KeyFileName;
        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
        defaultFolderKey.SetValue("", KeyFileName);
      }
    }

    private void txtSigningKey_TextChanged(object sender, EventArgs e) {
      cmdOK.Enabled = !(txtSigningKey.Text.Length == 0);
    }

  }
}